<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="支付申请编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入支付申请编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdate">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.createdate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="种植户" prop="plantorguid">
        <el-select v-model="queryParams.plantorguid" filterable clearable placeholder="请选择种植户" size="small">
          <el-option
            v-for="item in plantors"
            :key="item.id"
            :label="item.realname"
            :value="item.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发票状态" prop="invoiceon">
        <el-select v-model="queryParams.invoiceon" placeholder="请选择是否收到发票" clearable size="small">
          <el-option
            v-for="dict in invoiceonOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="payinfoList" :id="gid" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="支付申请编号" align="center" prop="code" width="100" />
      <el-table-column label="采收主体" align="center" prop="orgname" />
      <el-table-column label="收款人" align="center" prop="plantorname" width="120" />
      <el-table-column label="车数" align="center" prop="carnum" />
      <el-table-column label="结算金额" align="center" prop="amount" />
<!--      <el-table-column label="已付金额" align="center" prop="preamount" />-->
      <el-table-column label="付款金额" align="center" prop="applyAmount" />
      <el-table-column label="交易后未付" align="center" prop="recamount" />
      <el-table-column label="创建时间" align="center" prop="createdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="operatorrealname" />
      <el-table-column label="发票状态" align="center" prop="invoiceon" :formatter="invoiceonFormat" />
      <el-table-column label="状态" align="center" prop="statusname" />
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--          >详情</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listPayinfo, getPayinfo,getPlanters } from "@/api/pay/payinfo";
import freightorderList from "@/views/pay/payinfo/freightorderList";

export default {
  name: "Payinfo",
  components: {
    freightorderList
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付表格数据
      payinfoList: [],
      statusOptions: [],
      invoiceonOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createdate: null,
        code: null,
        plantorguid: null,

        invoiceon: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "支付申请编号不能为空", trigger: "blur" }
        ],
      },
      plantors: '',
      gid:'',
    };
  },
  created() {
    this.getList();
    this.getPlantors();
    this.getDicts("pay_harvest_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("pay_invoiceon").then(response => {
      this.invoiceonOptions = response.data;
    });
  },
  methods: {
    /** 查询支付列表 */
    getList() {
      this.loading = true;
      listPayinfo(this.queryParams).then(response => {
        this.payinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 是否收到发票:1 收到 或 2 未收到字典翻译
    invoiceonFormat(row, column) {
      return this.selectDictLabel(this.invoiceonOptions, row.invoiceon);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        guid: null,
        amount: null,
        status: null,
        createdate: null,
        operatorid: null,
        lastupdate: null,
        code: null,
        plantorguid: null,
        carnum: null,
        weight: null,
        departid: null,
        departname: null,
        flowid: null,
        preamount: null,
        recamount: null,
        srguid: null,
        bank: null,
        bankcode: null,
        remark: null,
        orgid: null,
        orgname: null,
        invoiceon: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.guid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 支付详情 */
    handleUpdate(row) {
      const guid = row.guid
      this.$router.push({path:"/harvest/payinfoforders", query:{"id": guid}});
    },
    getPlantors: function () {
      getPlanters().then(response => {
        this.plantors = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.guid != null) {
            updatePayinfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPayinfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
